package appli.calcul;

import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Recherche extends Activity{

	private Spinner zone;
	private Spinner cat;
	private Spinner nom;
	private Button valider;

	private static final int MON_CODE = 1;

	private DatabaseAdaptateur mabd;

	private boolean selectcat = false;
	private boolean selectzone = false;

	private InputStream is;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.recherche);

		zone = (Spinner) findViewById(R.id.zone);
		cat = (Spinner) findViewById(R.id.categorie);
		nom = (Spinner) findViewById(R.id.nombat);
		valider = (Button) findViewById(R.id.btrecherche);
		is = getResources().openRawResource(R.raw.install);
		this.mabd = new DatabaseAdaptateur(getApplicationContext());

		mabd.open();

		String[] statements = null;
		try{
			statements = FileHelper.parseSqlFile(is);

			for (String statement : statements) {
				mabd.insertsql(statement);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}



		remplirspinnernom();
		remplirspinnercat();
		remplirspinnerzone();

		final boolean iti = getIntent().getExtras().getBoolean("iti");
		//créer le listener du bouton recherche 

		final Activity act = this;


		valider.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				if(iti)
				{
					Intent recherche = new Intent();
					recherche.putExtra("iti", iti);
					recherche.putExtra("nom", nom.getSelectedItem().toString());
					recherche.putExtra("zone", zone.getSelectedItem().toString());
					recherche.putExtra("cat", cat.getSelectedItem().toString());
					setResult(RESULT_OK, recherche);
					finish();
				}
				else
				{
					Intent recherche = new Intent(act, Resultat.class);
					recherche.putExtra("iti", iti);
					recherche.putExtra("nom", nom.getSelectedItem().toString());
					act.startActivityForResult(recherche, MON_CODE);
				}
			}
		});

		zone.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if(arg2 != 0)
				{
					if(selectcat)
					{
						Cursor c = mabd.selectbycatandid(cat.getSelectedItem().toString(), zone.getSelectedItem().toString());
						if(c.getCount() !=0)
						{
							c.moveToFirst();
							String[] res1 = new String[c.getCount()+1];
							int i = 0;
							res1[i] = "--Nom";
							do
							{
								i++;
								res1[i] = c.getString(0);
							}
							while(c.moveToNext());
							modifspinner(res1);
							c.close();
						}
						else
						{
							String[] res1 = new String[1];
							res1[0] = "--Nom";
							modifspinner(res1);
						}
					}
					else
					{
						String z = zone.getSelectedItem().toString();
						Cursor c = mabd.selectbyzone(z);
						if(c.getCount() !=0)
						{
							c.moveToFirst();
							String[] res1 = new String[c.getCount()+1];
							int i = 0;
							res1[i] = "--Nom";
							do
							{
								i++;
								res1[i] = c.getString(0);
							}
							while(c.moveToNext());
							modifspinner(res1);
							c.close();
							selectzone = true;
						}
						else
						{
							String[] res1 = new String[1];
							res1[0] = "--Nom";
							modifspinner(res1);
							selectzone = true;
						}
					}
				}
				else
					selectzone = false;
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		cat.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if(arg2 != 0)
				{
					if(selectzone)
					{
						Cursor c = mabd.selectbycatandid(cat.getSelectedItem().toString(), zone.getSelectedItem().toString());
						if(c.getCount() !=0)
						{
							c.moveToFirst();
							String[] res1 = new String[c.getCount()+1];
							int i = 0;
							res1[i] = "--Nom";
							do
							{
								i++;
								res1[i] = c.getString(0);
							}
							while(c.moveToNext());
							modifspinner(res1);
							c.close();
						}
						else
						{
							String[] res1 = new String[1];
							res1[0] = "--Nom";
							modifspinner(res1);
						}
					}
					else
					{
						Cursor c = mabd.selectbycat(cat.getSelectedItem().toString());
						if(c.getCount() !=0)
						{
							c.moveToFirst();
							String[] res1 = new String[c.getCount()+1];
							int i = 0;
							res1[i] = "--Nom";
							do
							{
								i++;
								res1[i] = c.getString(0);
							}
							while(c.moveToNext());
							modifspinner(res1);
							c.close();
							selectcat = true;
						}
						else
						{
							String[] res1 = new String[1];
							res1[0] = "--Nom";
							modifspinner(res1);
						}
					}
				}
				else
					selectcat = false;
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void remplirspinnernom() {
		// TODO Auto-generated method stub
		Cursor c = mabd.selectallbat();

		String[] res = new String[c.getCount()+1];

		if(c.getCount() != 0)
		{
			c.moveToFirst();
			Integer i = 0;
			res[i] = "--Nom";
			do
			{
				i++;
				res[i] = c.getString(0);
			}
			while(c.moveToNext());
		}
		ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, res);
		adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		nom.setAdapter(adapt);
		c.close();
	}
	
	private void remplirspinnercat()
	{
		Cursor c1 = mabd.selectallcat();

		String[] res1 = new String[c1.getCount()+1];

		if(c1.getCount() != 0)
		{
			c1.moveToFirst();
			Integer i = 0;
			res1[i] = "--Categorie";
			do
			{
				i++;
				res1[i] = c1.getString(0);
			}
			while(c1.moveToNext());
		}
		ArrayAdapter<String> adapt1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, res1);
		adapt1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		cat.setAdapter(adapt1);
		c1.close();
	}
	
	private void remplirspinnerzone()
	{
		Cursor c2 = mabd.selectallzone();

		String[] res2 = new String[c2.getCount()+1];

		if(c2.getCount() != 0)
		{
			c2.moveToFirst();
			Integer i = 0;
			res2[i] = "--Zone";
			do
			{
				i++;
				res2[i] = c2.getString(0);
			}
			while(c2.moveToNext());
		}
		ArrayAdapter<String> adapt2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, res2);
		adapt2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		zone.setAdapter(adapt2);
		c2.close();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		mabd.close();
		super.onBackPressed();
	}

	public InputStream getFic()
	{
		return is;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		mabd.close();
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		mabd.close();
		super.onStop();
	}

	public void modifspinner(String[] res)
	{
		ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, res);
		adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		nom.setAdapter(adapt);

	}

	protected void onActivityResult(int ident, int code_retour, Intent retour)
	{
		switch(ident)
		{
		case MON_CODE : 
			Intent ret = new Intent();
			ret.putExtra("id", retour.getExtras().getInt("id"));
			ret.putExtra("lat", retour.getExtras().getDouble("lat"));
			ret.putExtra("lon", retour.getExtras().getDouble("lon"));
			setResult(RESULT_OK, ret);
			finish();
			return;

		}
	}	
}
